/**
 * Created by Administrator on 2017/3/23.
 */

var App=function () {
    //上传通用地址
    this.upload_url='/api/index/uploadImage';
//上传
	this.uploadFile=function (obj)
	{
		var self=this;
		//初始化数据
		obj.each(function (k,v)
				 {
					 var name=$(this).attr('name');
					 var val=$(this).attr('value');
				 	var panel='<div class="image-upload"></div>';
				 	$(this).wrap(panel);
					 $(this).parent().append('<div class="image"><img src="'+val+'"><a class="remove"><i class="fa fa-remove"></i></a></div><input type="file" class="hide">');
				 });
		//选择图片自动上传
		$("body").on("change",".image-upload [type='file']",function ()
		{
			var image_panel=$(this).prev();
			var _panel=$(this).parent();
			var formData = new FormData();
			var file=$(this)[0].files[0];
            $(this)[0].files[0]=null;
			if(file.length ==0){
				return;
			}
			formData.append('file',file);
			var url =self.upload_url;
			var load=layer.load(2);
			$.ajax({
					   url: url,
					   type: 'POST',
					   data: formData,
					   async: true,
					   cache: false,
					   contentType: false,
					   processData: false,
					   success:function (res)
					   {
						   layer.close(load);
						   if(res.code==1){
							   image_panel.find("img").attr("src",res.data);
							   _panel.find(".m-upload").val(res.data);
						   }else{
							   layer.msg("上传失败");
						   }
					   },
					   error:function (e)
					   {
						   layer.close(load);
						   layer.msg("网络出错")
					   }
				   });
		});
		//触发选择
		$("body").on("click",".image-upload .image",function ()
		{
			$(this).parent().find("[type='file']").click();
		});
		//移除当前图片
		$("body").on("click",".image-upload  .remove",function (event)
		{
			var _panel=$(this).parent().parent();
			$(this).prev("img").attr("src",'');
			_panel.find(".m-upload").val('');
			event.stopPropagation();
		});
	};
    this.ajaxForm=function (form,sFunc,eFunc) {
		
        var _btn=form.find("button[type='submit']");
        var url=form.attr('action');
      	var load=layer.load(2);
		_btn.attr("disabled",true);
        //成功函数
        function completeHandler(res) {
        	layer.close(load);
            if(sFunc){
                sFunc(res);
            }else{
                if(res.code==0){
                    layer.msg(res.msg?res.msg:'操作失败');
                    _btn.attr('disabled',false);
                }else{
                    layer.msg(res.msg,{time:1200},function () {
                        location.reload();
                    });
                }
            }
        }
        //错误函数
        function errorHandler(e) {
			layer.close(load);
            if(eFunc){
                eFunc(e);
            }else{
                layer.msg('网络错误');
                _btn.attr('disabled',false);
            }
        
        }
        
        if(form.attr("enctype")=="multipart/form-data"){
            var formData = new FormData(form[0]);
            $.ajax({
                       url: url, 
                       type: 'POST',
                       data: formData,
                       async: true,
                       cache: false,
                       contentType: false,
                       processData: false,
                       success:function (res)
                       {
                           completeHandler(res);
                       },
                       error:function (e)
                       {
                           errorHandler(e);
                       }
                   });
        }else{
            $.ajax({
                       url:url,
                       type:'post',
                       data:form.serialize(),
                       dataType:'json',
                       success:function (res)
                       {
                           completeHandler(res);
                       },
                       error:function (e)
                       {
                           errorHandler(e);
                       }
                   });
        }
    };

    this.deleteHandle=function (url,sFunc,eFunc)
    {
      this.confirm("确认删除？",url,sFunc,eFunc);
    };
    this.confirm=function (msg,url,sFunc,eFunc)
    {
      layer.confirm(msg, function ()
      {
		  var load=layer.load(2);
        $.ajax({
                 url : url,
                 type : 'post',
                 data : { auth_token : '' },
                 dataType : 'json',
                 success : function (res)
                 {
					 layer.close(load);
                   if (sFunc) {
                     sFunc(res);
                   } else {
                     layer.msg(res.msg, { time : 800 },function () {
                       location.reload();
                     });
                   }
                 },
                 error : function (e)
                 {
					 layer.close(load);
					if (eFunc) {
					 eFunc(e);
					} else {
					 layer.msg('网络错误');
					}
                 }
               });
      });
    };
    this.openIFrame=function (title,url,area,eFunc)
    {
        area= area?area:['450px', '550px'];
        layer.open({
                       type: 2,
                       title: title,
                       closeBtn: 1, //不显示关闭按钮
                       shade: [0.5],
                       area: area,
                       offset: 't', //右下角弹出
                       time: 0, //2秒后自动关闭
                       anim: 2,
                       content: [url,'yes'], //iframe的url，no代表不显示滚动条
                       end: function(){
                           if(eFunc){
                               eFunc();
                           }else{
                               location.reload();
                           }
                       }
                   });
    }
};